package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysDevice;
import com.ruoyi.system.domain.SysSupplier;
import com.ruoyi.system.service.ISysDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 物资信息Controller
 * 
 * @author ruoyi
 * @date 2022-07-19
 */
@CrossOrigin
@Api("物资信息管理")
@Controller
@RequestMapping("/system/device")
public class SysDeviceController extends BaseController
{
    private String prefix = "system/device";

    @Autowired
    private ISysDeviceService sysDeviceService;

//    @RequiresPermissions("system:device:view")
    @GetMapping()
    public String device()
    {
        return prefix + "/device";
    }

    /**
     * 查询物资信息列表
     */
    @ApiOperation("查询物资信息列表")
//    @RequiresPermissions("system:device:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysDevice sysDevice)
    {
        startPage();
        List<SysDevice> list = sysDeviceService.selectSysDeviceList(sysDevice);
        return getDataTable(list);
    }

    /**
     * 导出物资信息列表
     */
    @ApiOperation("导出物资信息列表")
//    @RequiresPermissions("system:device:export")
    @Log(title = "物资信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysDevice sysDevice)
    {
        List<SysDevice> list = sysDeviceService.selectSysDeviceList(sysDevice);
        ExcelUtil<SysDevice> util = new ExcelUtil<SysDevice>(SysDevice.class);
        return util.exportExcel(list, "物资信息数据");
    }

    /**
     * 新增物资信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存物资信息
     */
    @ApiOperation("新增保存物资信息")
//    @RequiresPermissions("system:device:add")
    @Log(title = "物资信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysDevice sysDevice)
    {
        return toAjax(sysDeviceService.insertSysDevice(sysDevice));
    }

    /**
     * 修改物资信息
     */
//    @RequiresPermissions("system:device:edit")
    @GetMapping("/edit/{deviceId}")
    public String edit(@PathVariable("deviceId") Long deviceId, ModelMap mmap)
    {
        SysDevice sysDevice = sysDeviceService.selectSysDeviceByDeviceId(deviceId);
        mmap.put("sysDevice", sysDevice);
        return prefix + "/edit";
    }

    /**
     * 修改保存物资信息
     */
    @ApiOperation("修改保存物资信息")
//    @RequiresPermissions("system:device:edit")
    @Log(title = "物资信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysDevice sysDevice)
    {
        return toAjax(sysDeviceService.updateSysDevice(sysDevice));
    }

    /**
     * 删除物资信息
     */
    @ApiOperation("删除物资信息")
//    @RequiresPermissions("system:device:remove")
    @Log(title = "物资信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysDeviceService.deleteSysDeviceByDeviceIds(ids));
    }

    /**
     * 导入物资信息
     */
    @ApiOperation("导入物资信息")
//    @RequiresPermissions("system:device:import")
    @Log(title = "物资信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(@RequestPart MultipartFile file, @RequestParam(defaultValue = "false") boolean updateSupport) throws Exception
    {
        ExcelUtil<SysDevice> util = new ExcelUtil<SysDevice>(SysDevice.class);
        List<SysDevice> list = util.importExcel(file.getInputStream());
        String message = sysDeviceService.importDevice(list, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    /**
     * 导出物资信息模板
     */
    @ApiOperation("导出物资信息模板")
//    @RequiresPermissions("system:device:view")
    @Log(title = "物资信息", businessType = BusinessType.EXPORT)
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<SysDevice> util = new ExcelUtil<SysDevice>(SysDevice.class);
        return util.importTemplateExcel("物资信息");
    }
}
